//
//  CALEatingViewController.m
//  CalCounter
//
//  Created by Toanpro on 9/8/13.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALEatingViewController.h"
#import "CustomTableViewCell1.h"
#import "DataHelper.h"
#import "NSDate+Ex.h"
#import "DateTimeView.h"
#import "CALFood.h"
#import "CALFoodsViewController.h"
#import "CALFoodAmountViewController.h"
@interface CALEatingViewController ()<CALFoodAmountViewControllerDelegate>{
    __weak IBOutlet UITableView *_tableView;
    NSMutableArray *_foodsArray;
    
}

@end

@implementation CALEatingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFoodTapped:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.title = self.currentEating.eatingName;    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     _foodsArray = [NSMutableArray arrayWithArray:[[DataHelper sharedDatabase] getFoodsWithEatingId:self.currentEating.eatingId]];
    [_tableView reloadData];
}

- (void)addFoodTapped:(id)sender{
    CALFoodsViewController *vc = [[CALFoodsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate, UITableViewDatasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_foodsArray count] + 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == [_foodsArray count]){   //last row
        static NSString *identifier = @"last_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
        [cell.textLabel setText:NSLocalizedString(@"taphere", nil)];
        [cell.textLabel setFont:[UIFont systemFontOfSize:13]];
        [cell.textLabel setTextColor:[UIColor grayColor]];
        return cell;
    }else{
        static NSString *identifier = @"cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
        
        CALFood *f = [_foodsArray objectAtIndex:indexPath.row];
        cell.textLabel.text = f.foodName;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%1.1f cals",f.cals];
        cell.imageView.image = [UIImage imageNamed:f.foodImage];
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == [_foodsArray count]){
        CALFoodsViewController *vc = [[CALFoodsViewController alloc] init];
        vc.isSearchMode = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        CALFoodAmountViewController *vc = [[CALFoodAmountViewController alloc] init];
        vc.selectedFood = [_foodsArray objectAtIndex:indexPath.row];
        vc.isEditMode = YES;
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CALFood *f = [_foodsArray objectAtIndex:indexPath.row];
    [_foodsArray removeObject:f];
    [[DataHelper sharedDatabase] deleteFood:f eatingId:self.currentEating.eatingId];
    [tableView reloadData];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row != [_foodsArray count];
}
#pragma mark - CALFoodAmountViewControllerDelegate
- (void)didSelectFood:(CALFood *)food{
    [_foodsArray addObject:food];
}
- (NSInteger)eatingIdForUpdateFood{
    return self.currentEating.eatingId;
}
//#pragma mark - DateTimeViewDelegate
//- (void)whenChangeTime:(NSDate *)_date{
//    _foodsArray = [NSMutableArray arrayWithArray:[[DataHelper sharedDatabase] getFoodsWithEatingId:self.currentEating.eatingId inDate:[[DateTimeView selectDate] dateString]]];
//    [_tableView reloadData];
//    
//}
//#pragma mark -

@end
